﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.Integration.DAO.Impl;
using Demo.CommonBase.Logging;
using Demo.Services.Integration.DAO.Contracts.Exceptions;
using System.Data.SqlClient;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using System.Data;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Integration.DAO.IntegrationComponents.Helpers;
using Demo.Services.Integration.DAO.IntegrationComponents.Mappings;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;

namespace Demo.Services.Integration.DAO.Impl
{
    public class ResourceTypeDAO : DAOBase
    {
        public ResourceTypeResultInfo GetResourceTypeList(PaginationInfo pagin, ResourceTypeFilter filters)
        {
            ResourceTypeResultInfo resourceTypeList = null;
            try
            {
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetResourceTypeByPage(pagin, filters);
                DataSet ds = Retrieve("ResourceType_R", parameters);
                if (ValidateDataSet(ds))
                {
                    resourceTypeList = ResourceTypeDAOMapper.GetResourceTypeList(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return resourceTypeList;
        }

        /// <summary>
        /// Método para guardar un Tipo de recurso
        /// </summary>
        /// <param name="nombre"></param>
        public int SaveResourceType(ResourceTypeInfo info)
        {
            int id = 0;
            try
            {
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetSaveParameters(info);
                id = Create("ResourceType_C", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return id;
        }

        public void DeleteResourceType(int id)       
        {          
            try
            {
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetIdParameter(id);
                Delete("ResourceType_D", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }  

        public List<ResourceTypeInfo> GetList()
        {
            List<ResourceTypeInfo> resourceTypeList = null;
            try
            {                
                DataSet ds = Retrieve("ResourceType_R_List");
                if (ValidateDataSet(ds))
                {
                    resourceTypeList = ResourceTypeDAOMapper.GetList(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return resourceTypeList;
        }
        public ResourceTypeInfo GetResourceTypeById(int id)        
        {            
            ResourceTypeInfo result = null;            
            try            
            {                
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetIdParameter(id);                
                DataSet ds = Retrieve("ResourceType_ById", parameters);                
                if (ValidateDataSet(ds))               
                {                    
                    result = ResourceTypeDAOMapper.GetById(ds);                
                }            
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }
        public void UpdateResourceType(ResourceTypeInfo resourceType)
        {
            try
            {
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetUpdateParams(resourceType);
                Update("ResourceType_U", parameters);

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        public bool CheckName(int id, string name)
        {
            bool isValid = false;
            try
            {
                IList<IDbDataParameter> parameters = ResourceTypeDAOHelper.GetCheckNameParams(id, name);
                DataSet ds = Retrieve("ResourceType_R_CheckName", parameters);
                isValid = !ValidateDataSet(ds);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ResourceTypeDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ResourceTypeDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return isValid;
        }

    }
}
